//
//  PickerViewController.swift
//  TicketSystem
//
//  Created by ET|冰琳 on 2017/2/15.
//  Copyright © 2017年 UK. All rights reserved.
//

import UIKit
import ReactiveSwift
import Result

extension T {
    
    // MARK: - 联动picker
    static func showPickers<T>(from: UIViewController,
                            type: T.Type,
                            title: String? = nil,
                            componentCallback: @escaping PickerComponentCallBack,
                            rowCallback:  @escaping PickerRowsCallback,
                            textcallback: @escaping (_ component: Int, _ row: Int) -> String) -> SignalProducer<[Int], NoError> {
      
        return SignalProducer.init({ (observe, dispose) in
            
            let picker = PickerViewController()
            picker.titleLabel.text = title
            picker.componentCallback = componentCallback
            picker.rowsCallback      = rowCallback
            
            picker.resultBlock = { (results, indexes) in
                observe.send(value: indexes.map({ $0.intValue }))
                observe.sendCompleted()
            }
            
            picker.reactive.lifetime.ended.observeCompleted {
                observe.sendInterrupted()
            }
            
            picker.textCallback = { item in
                return textcallback(item.1, item.2)
            }

            from.present(picker, animated: true, completion: nil)
        })
    }
    
    // MARK: - 非联动数据的picker
    static func showPicker(from: UIViewController, datas: [String], select: String?, title: String? = nil) -> SignalProducer<(String, Int/*select row*/), NoError> {
        
        return SignalProducer.init({ (observe, dispose) in
            
            let picker = PickerViewController()
            picker.titleLabel.text = title
            
            picker.resultBlock = { (results, indexes) in
                
                let arrStr = results as! [String]
                if arrStr.count > 0{
                    observe.send(value: (arrStr[0], indexes[0] as Int))
                }
                observe.sendCompleted()
            }
            
            picker.reactive.lifetime.ended.observeCompleted {
                observe.sendInterrupted()
            }
            
        
            picker.textCallback = { item in
                return item.0 as! String
            }
            
            if let select = select {
                picker.selectInfo = [select]
            }
            
            picker.pickerDatas = [datas]

            from.present(picker, animated: true, completion: nil)
        })
    }
    
    static func showPickers<T>(from: UIViewController,
                            title: String? = nil,
                            datas: [[T]]?,
                            select: [T]?,
                            textCallback: @escaping (_ data: T, _ component: Int, _ row: Int) -> String)
        -> SignalProducer<[T], NoError> {
        
        return SignalProducer.init({ (observe, dispose) in
            
            let picker = PickerViewController()
            picker.titleLabel.text = title
            
            picker.resultBlock = { (results, indexes) in
                
                let arr = results as! [T]
                observe.send(value: arr)
                observe.sendCompleted()
            }
            
            picker.reactive.lifetime.ended.observeCompleted {
                observe.sendInterrupted()
            }
            
            picker.textCallback = { item in
                let data = item.0 as! T
                return textCallback(data, item.1, item.2)
            }
            
            if let selected = select {
                picker.selectInfo = selected
            }
            
            if let datas = datas {
                picker.pickerDatas = datas
            }
            
            from.present(picker, animated: true, completion: nil)
        })
    }
    
    
    static func showPicker<T>(from: UIViewController,
                            title: String? = nil,
                            datas: [T],
                            select: T?,
                            textCallback: @escaping (_ data: T, _ row: Int) -> String)
        -> SignalProducer<T, NoError>{
            
            var selected: [T]?
            if let select = select {
                selected = [select]
            }
            return self.showPickers(from: from, title: title, datas: [datas], select: selected, textCallback: { item in
                let t = textCallback(item.0, item.2)
                return t
            }).map { $0[0] }
            
    }
    
}
